import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import script.IScript;
import script.impl.BaseScript;

public class DynaCompTest {
    public static final Map<Class<? extends IScript>, BaseScript> scripts = new HashMap<Class<? extends IScript>, BaseScript>();

    public static void main(String[] args) throws Exception {
        Method[] methods = DynaCompTest.class.getDeclaredMethods();
        for (Method method : methods) {
            System.out.println(method.getName());
        }
        // String path = "E:/workspace/my/DynamicScript/src/script/impl/PlayerLoginScript.java";
        // File file = new File(path);
        // System.out.println(file.getName());
        // String[] str = ",1,,1,".split(",");
        // for (String s : str) {
        // System.out.println(s);
        // }
        // URL[] urls = ((URLClassLoader) DynaCompTest.class.getClassLoader()).getURLs();
        // for (URL url : urls) {
        // System.out.println(url.getFile());
        // System.out.println(url.getPath());
        // }
        // PlayerLoginScript.getScript().login();
        // String fullName = "DynaClass";
        // StringBuilder src = new StringBuilder();
        // src.append("public class DynaClass {\n");
        // src.append("    public String toString() {\n");
        // src.append("        return \"Hello, I am \" + ");
        // src.append("this.getClass().getSimpleName();\n");
        // src.append("    }\n");
        // src.append("}\n");
        //
        // System.out.println(src);
        // DynamicEngine de = DynamicEngine.getInstance();
        // Object instance = de.javaCodeToObject(fullName, src.toString());
        // System.out.println(instance);

        // scripts.put(IPlayerLoginScript.class, PlayerLoginScript.getScript());
        // while (true) {
        // ((IPlayerLoginScript) scripts.get(IPlayerLoginScript.class)).login();
        // System.out.println("reload重载脚本?");
        // Scanner scanner = new Scanner(System.in);
        // String oder = scanner.nextLine();
        // if ("reload".equals(oder)) {
        // URL[] urls = new URL[] { new URL("file:/" + System.getProperty("user.dir") + "/bin") };
        // URLClassLoader loader = new URLClassLoader(urls);
        // Class c = loader.loadClass("script.impl.PlayerLoginScript");
        // Object script = c.newInstance();
        // // DynamicEngine de = DynamicEngine.getInstance();
        // // String path = "E:/workspace/my/DynamicScript/src/script/impl/PlayerLoginScript.java";
        // // // String content = readFile(Thread.currentThread().getContextClassLoader().getResource("").toString() + "script/impl/PlayerLoginScript.java");
        // // String content = readFile(path);
        // // Object script = de.javaCodeToObject("script.impl.PlayerLoginScript", content);
        // scripts.put(IPlayerLoginScript.class, ((BaseScript) script));
        // }
        // }

        // scripts.put(IPlayerLoginScript.class, PlayerLoginScript.getScript());
        // while (true) {
        // ((IPlayerLoginScript) scripts.get(IPlayerLoginScript.class)).login();
        // System.out.println("reload重载脚本?");
        // Scanner scanner = new Scanner(System.in);
        // String oder = scanner.nextLine();
        // if ("reload".equals(oder)) {
        // CustomCL loader = new CustomCL(file.getPath(), new String[] { "script.impl.PlayerLoginScript" });
        // Class<?> c = loader.loadClass("script.impl.PlayerLoginScript");
        // Object script = c.newInstance();
        // // DynamicEngine de = DynamicEngine.getInstance();
        // // String path = "E:/workspace/my/DynamicScript/src/script/impl/PlayerLoginScript.java";
        // // // String content = readFile(Thread.currentThread().getContextClassLoader().getResource("").toString() + "script/impl/PlayerLoginScript.java");
        // // String content = readFile(path);
        // // Object script = de.javaCodeToObject("script.impl.PlayerLoginScript", content);
        // if (script instanceof BaseScript) {
        // scripts.put(IPlayerLoginScript.class, ((BaseScript) script));
        // }
        // }
        // }

    }

    public static String readFile(String path) {
        StringBuilder content = new StringBuilder();
        if (path != null) {
            // path = path.replace("/bin", "/src");
            File file = new File(path);
            if (file.getName().endsWith(".java")) {
                BufferedReader scanner;
                try {
                    scanner = new BufferedReader(new FileReader(file));
                    String line = null;
                    try {
                        while ((line = scanner.readLine()) != null) {
                            content.append(line).append(System.lineSeparator());
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
        return content.toString();
    }
}